package com.taokatao.cardmee.business;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;

import com.taokatao.cardmee.ActivityCollector;
import com.taokatao.cardmee.BaseActivity;
import com.taokatao.cardmee.BusEvent;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.util.Once;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MainActivity extends BaseActivity implements PrimaryMenuLayout.Callback, CardLayout.Callback {

    @BindView(R.id.l_primary_menu) PrimaryMenuLayout lPrimaryMenu;
    @BindView(R.id.l_once) FrameLayout lOnce;
    @BindView(R.id.l_card) CardLayout lCard;
    @BindView(R.id.l_profile) ProfileLayout lProfile;
    @BindView(R.id.l_more) MoreLayout lMore;
    private View mCurrPrimaryLayout;

    private long mExitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        lPrimaryMenu.setCallback(this);
        lPrimaryMenu.clickMenu(PrimaryMenuLayout.Menu.Card);
        lCard.setCallback(this);
        lProfile.setCallback(lCard);
    }

    @Override
    public void onPrimaryMenuClick(int selected) {
        switch (selected) {
            case PrimaryMenuLayout.Menu.Card:
                lCard.setVisibility(View.VISIBLE);
                mCurrPrimaryLayout = lCard;
                lMore.setVisibility(View.GONE);
                lProfile.setVisibility(View.GONE);

                Once.coverMainCard(lOnce, lCard.obtainMyCardCount() + lCard.obtainMyCollectCount());
                break;
            case PrimaryMenuLayout.Menu.More:
                lCard.setVisibility(View.GONE);
                lMore.setVisibility(View.VISIBLE);
                lProfile.setVisibility(View.GONE);
                mCurrPrimaryLayout = lMore;
                lMore.loadRootPageJustFirstTime();
                break;
            case PrimaryMenuLayout.Menu.Profile:
                lCard.setVisibility(View.GONE);
                lMore.setVisibility(View.GONE);
                lProfile.setVisibility(View.VISIBLE);
                mCurrPrimaryLayout = lProfile;
                break;
        }
    }

    @Override
    public void onPrepareDataCompleted() {
        if (mCurrPrimaryLayout == lCard) {
            Once.coverMainCard(lOnce, lCard.obtainMyCardCount() + lCard.obtainMyCollectCount());
        }
    }

    @Override
    protected void onPictureQueryOut(String path) {
        super.onPictureQueryOut(path);
        if (TextUtils.equals("profile_avatar_background", picTag)) {
            lProfile.setAvatarBackground(path);
        }
    }

    @Override
    public void onBackPressed() {
        final long currentTime = System.currentTimeMillis();
        if (currentTime - mExitTime > 2000) {
            mExitTime = System.currentTimeMillis();
        } else {
            ActivityCollector.finishAll();
        }
    }

    @Override
    public void onEventMainThread(BusEvent event) {
        super.onEventMainThread(event);
        if (event.type == BusEvent.TYPE_MAIN_THREAD) {
            switch (event.action) {
                case BusEvent.WEB_RETURN_TO_HOME_CARD:
                case BusEvent.USER_LOGOUT:
                    lPrimaryMenu.clickMenu(PrimaryMenuLayout.Menu.Card);
                    break;
            }
        }
    }
}
